package com.competition.reception.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.commonutils.R;
import com.competition.reception.entity.ExaminationDetail;
import com.competition.reception.entity.vo.TopicVo;
import com.competition.reception.listenter.Listenter;
import com.competition.reception.mapper.ExaminationDetailMapper;
import com.competition.reception.service.ExaminationDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * <p>
 * 知识竞赛题目表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-05
 */
@Service
public class ExaminationDetailServiceImpl extends ServiceImpl<ExaminationDetailMapper, ExaminationDetail> implements ExaminationDetailService {

    @Autowired
    private ExaminationDetailService examinationDetailService;


    @Override
    public List<ExaminationDetail> getExaminationSubject(String examinationId) {
        QueryWrapper<ExaminationDetail> examinationDetailQueryWrapper=new QueryWrapper<>();
        examinationDetailQueryWrapper.eq("examination_id",examinationId);
        List<ExaminationDetail> examinationDetails = baseMapper.selectList(examinationDetailQueryWrapper);
        return examinationDetails;
    }

    @Override
    public R uploadToptic(MultipartFile file) {
        //对文件进行解析并返回题目资料
        List<TopicVo> options = returnOptions(file);
        String examinationId= UUID.randomUUID().toString().substring(0, 18);
        //存储试题详细资料
        List<ExaminationDetail> examinationDetails = new ArrayList<>();
        //遍历返回的option试题资料并copy到examinationDetail中
        for (int i = 0; i < options.size(); i++){
            ExaminationDetail examinationDetail=new ExaminationDetail();
            examinationDetail.setExaminationId(examinationId);
            BeanUtils.copyProperties(options.get(i), examinationDetail);
            examinationDetails.add(examinationDetail);
        }
        //批量插入数据
        examinationDetailService.saveBatch(examinationDetails);
        Map<String,Object> map=new HashMap<>();
        map.put("examinationDetails",examinationDetails);
        map.put("topicNum", examinationDetails.size());
        map.put("examinationId",examinationId);
        map.put("options",options);
        options.clear();
        return R.ok().data(map);
    }

//    @Override
//    public void deleteTopicByExaminationId(String examinationId) {
//        Map<String,Object> map=new HashMap<>();
//        map.put("examination_id",examinationId);
//        baseMapper.deleteByMap(map);
//    }

    public static List<TopicVo> returnOptions(MultipartFile file){
        InputStream inputStream = null;
        try {
            //获取文件输出流
            inputStream = file.getInputStream();
            InputStream in = new BufferedInputStream(inputStream);
            //读取文件解析
            ExcelReader excelReader = EasyExcel.read(in, TopicVo.class, new Listenter()).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            //返回题目资料
            List<TopicVo> options = Listenter.returnOptions();
            // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
            excelReader.finish();
            return options;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
